<template>
	<view>
		<mescroll-body style="
		white-space: nowrap;
		overflow-x: scroll;" ref="mescrollRef" @init="mescrollInit" @down="downCallback"
			 :up="upOption" @up="upCallback">  
				<wTable ref="mychild"  :columns="columns" :data1="data" border 
				 @on-select-all="selectAll" @on-select="selectO">
				</wTable>
			</mescroll-body>
			<view class="box">
				<view :style="bj" class="buttonBox2" @click="Edit()">
					设置
				</view>
			</view>

			<mask-model ref="showMask" btnType="1" :maskTitle="maskTitle" :titleContext="titleContext" @confirm="confirm"
				@cancel="cancel" :titleColoe="color" :cancelColor="color" :confirmColor="color"></mask-model>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import wTable from '../../components/wang-wTable/wTable.vue';
	import maskModel from '../../components/mask-model/mask-model.vue'
	import { encryptDes, decryptDes  } from '@/common/des.js' 
	
	export default {
		components: {
		wTable,
		maskModel
		},
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		data() {
			return {
			bor:{
			border:'1px dashed '+this.$kdup.color
			},
			color:this.$kdup.color,
			titleContext: "",
			maskTitle: "",
			tabindex: 0,
			bj: {
				backgroundColor: this.$kdup.color,
			},
			data: [], // 数据列表
			curWord: "", //当前搜索关键词
			SelectList: [],
			columns: [],
			goods:[],
			upOption: {
				auto: false, //是否在初始化后,自动执行上拉回调callback; 默认true
				noMoreSize: 3, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
				empty: {
					tip: '~ 搜索无结果 ~' // 提示
				}
			},
			}
		},
		onBackPress(e){ 
		  uni.navigateTo({
		    url:"/pages/bill/bill"
		  })
		  // 此处一定姚要return为true，否则页面不会返回到指定路径
		  return true;
		},
		onShow() {
			uni.setNavigationBarColor({
				frontColor: "#ffffff",	//文字颜色
				backgroundColor: this.$kdup.color	//底部背景色
			})
		},
		onLoad() {
	     this.init();
		},
		methods: {
			init(){
				var str = "";
				//所有临时表
				str = this.$kdup.getEntryBarcodeSql(0, 5,"");
				var data = {
					sql: str
				}
				this.$myRequest.postRequest("LoadListJson", data).then(res => {
					if (res.data.error == "") {
						//找到数据
						var array = this.$kdup.split(res.data.Table[res.data.Table.length - 1].ffieldorder, ";");
						// 枚举值是13开启复选
						var headArray = this.$kdup.getTableHead(array, true);
						var valueArray = this.$kdup.getTableContent(headArray, res.data.Table);
						this.columns = headArray;
						this.goods = valueArray;
					} else {
						//未找到数据
						uni.showToast({
							title: res.data.error,
							icon: "none"
						});
					}
					//这里是刷新数据
					this.mescroll.resetUpScroll();
				})
			},
			cancel: function() { //取消按钮
				this.mescroll.resetUpScroll();
				this.init();
				uni.showToast({
					title: '已取消',
					mask: false,
					duration: 1500,
					icon: "none"
				});
			},
			confirm: function(data) { 
				this.$myRequest.postRequest("LoadListJson", data).then(res => {
					if (res.data.hasOwnProperty("error") && !this.$kdup.isEmpty(res.data.error)) {
						this.init();
						this.mescroll.resetUpScroll();
						uni.showToast({
							title: "删除成功！",
							icon: "none"
						});
					} else {
						uni.showToast({
							title: res.data.error,
							icon: "none"
						});
					}
				})
			},

			Edit:function(number){
			if(this.SelectList.length==0){
					uni.showToast({
						title: "请选择要设置的单据！",
						icon: "none"
					});
				} else if (this.SelectList.length==undefined ) {
					if(this.SelectList.allChecked.length>1){
					uni.showToast({
						title: "每次只能设置一个单据！",
						icon: "none"
					});	
					}else{
						uni.showToast({
							title: "请选择要设置的单据！",
							icon: "none"
						});
					}
				} else if(this.SelectList.length>1){
					uni.showToast({
						title: "每次只能设置一个单据！",
						icon: "none"
					});	
				}else{
                 var fid = "";var fname="";
				 fid =  this.SelectList.map(function(elem, index) {	return  elem.id;}).join(",");
				 fname = this.SelectList.map(function(elem, index) {return  elem.fname;}).join(","); 
				uni.navigateTo({           //单据id 123674  名称 采购入库(源单)
					url: '/pages/BillTypeConfig/BillTypeConfig?id=' +fid + "&name=" +fname
				})	
				}
			},
		selectAll: function(allChecked) {
			this.SelectList = allChecked;
		},
		selectO: function({allChecked,checked}) {
			this.SelectList = allChecked;
		},
			clickrow: function({
				row
			}, index) {
			},
			upCallback: function(page) {
				//联网加载数据
				this.apiSearch().then(res => {
					//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
					this.mescroll.endSuccess(res.length);
					this.data = res;
					this.SelectList=[];
					this.$refs.mychild.isfalse(this.data);
				}).catch(() => {
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			}, //这里负责分页延迟显示
			apiSearch: function(pageNum, pageSize, keyword) {
					return new Promise((resolute, reject) => {
						//延时一秒,模拟联网
						setTimeout(() => {
							try {
								var str = "";
								//所有临时表
								str = this.$kdup.getEntryBarcodeSql(0, 5,"");
								var data = {
									sql: str
								}
								this.$myRequest.postRequest("LoadListJson", data).then(res => {
									if (res.data.error == "") {
										//找到数据
										var array = this.$kdup.split(res.data.Table[res.data
											.Table.length - 1].ffieldorder, ";");
										// 枚举值是13开启复选
										var headArray = this.$kdup.getTableHead(array, true);
										var valueArray = this.$kdup.getTableContent(headArray,
										res.data.Table);
										
										resolute(valueArray);
									} else {
										//未找到数据
										uni.showToast({
											title: res.data.error,
											icon: "none"
										});
										resolute([]);
									}
								})
			
							} catch (e) {
								reject(e);
							}
						}, 1000)
					})
				} 
		}
	}
</script>

<style>
	.box {
		width: 100%;
		height: 84upx;
		display: flex;
		justify-content: center;
		flex-direction: row;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 999;
	
	}
	.buttonBox2 {
		margin-left: 2rpx;
		width: 100%;
		height: 84upx;
		color: white;
		display: flex;
		align-items: center;
		justify-content: center;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 999;
	}
</style>
